package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.core.domain.PageWapper;
import com.xjgzinfo.usdp.core.domain.ResponsePage;
import com.xjgzinfo.usdp.core.domain.ResponseResult;
import com.xjgzinfo.usdp.service.BladeVisualMapQueryService;
import com.xjgzinfo.usdp.service.BladeVisualMapService;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.Optional;

/**
 * REST controller for managing {@link com.xjgzinfo.usdp.domain.BladeVisualMap}.
 */
@RestController
@RequestMapping("/blade-visual")
public class MapResource {

    private static final String ENTITY_NAME = "AvueServer4jAppBladeVisualMap";
    private final Logger log = LoggerFactory.getLogger(MapResource.class);
    private final BladeVisualMapService bladeVisualMapService;
    private final BladeVisualMapQueryService bladeVisualMapQueryService;
    @Value("${spring.application.name:usdp-app-unkown}")
    private String applicationName;

    public MapResource(BladeVisualMapService bladeVisualMapService, BladeVisualMapQueryService bladeVisualMapQueryService) {
        this.bladeVisualMapService = bladeVisualMapService;
        this.bladeVisualMapQueryService = bladeVisualMapQueryService;
    }

    /**
     * {@code POST  /map/save} : 创建实体 bladeVisualMap.
     *
     * @param bladeVisualMapDTO the bladeVisualMapDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new bladeVisualMapDTO, or with status {@code 400 (Bad Request)} if the bladeVisualMap has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/map/save")
    public ResponseResult<BladeVisualMapDTO> createBladeVisualMap(@Valid @RequestBody BladeVisualMapDTO bladeVisualMapDTO) throws URISyntaxException {
        log.debug("REST request to save BladeVisualMap : {}", bladeVisualMapDTO);
        if (bladeVisualMapDTO.getId() != null) {
            return new ResponseResult(200, null, "ID已存在", false);
        }
        BladeVisualMapDTO result = bladeVisualMapService.save(bladeVisualMapDTO);
        if (result.getId() != null) {
            return new ResponseResult(200, result, "创建成功", true);
        } else {
            return new ResponseResult(400, result, "创建失败", false);
        }
    }

    /**
     * {@code PUT  /map/update} : 修改实体 bladeVisualMap.
     *
     * @param bladeVisualMapDTO the bladeVisualMapDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated bladeVisualMapDTO,
     * or with status {@code 400 (Bad Request)} if the bladeVisualMapDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the bladeVisualMapDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/map/update")
    public ResponseResult<BladeVisualMapDTO> updateBladeVisualMap(@Valid @RequestBody BladeVisualMapDTO bladeVisualMapDTO) throws URISyntaxException {
        log.debug("REST request to update BladeVisualMap : {}", bladeVisualMapDTO);
        if (bladeVisualMapDTO.getId() == null) {
            return new ResponseResult(200, null, "ID不能为空", false);
        }
        BladeVisualMapDTO result = bladeVisualMapService.save(bladeVisualMapDTO);
        return new ResponseResult(200, result, "修改成功", true);
    }

    /**
     * {@code GET  /map/list} : 分页获取实体列表 bladeVisualMaps.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualMaps in body.
     */
    @GetMapping("/map/list")
    public ResponsePage<BladeVisualMapDTO> getAllBladeVisualMaps(BladeVisualMapCriteria criteria, Pageable pageable) {
        log.debug("REST request to get BladeVisualMaps by criteria: {}", criteria);
        Page<BladeVisualMapDTO> page = bladeVisualMapQueryService.findByCriteria(criteria, pageable);
        PageWapper<BladeVisualMapDTO> pageWapper = new PageWapper(page.getNumber(), page.getTotalPages(), page.getContent(), page.getSize(), page.getTotalElements());
        return new ResponsePage(200, pageWapper, "", true);
    }

    /**
     * {@code GET  /map/count} : count all the bladeVisualMaps.
     *
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
     */
    @GetMapping("/map/count")
    public ResponseEntity<Long> countBladeVisualMaps(BladeVisualMapCriteria criteria) {
        log.debug("REST request to count BladeVisualMaps by criteria: {}", criteria);
        return ResponseEntity.ok().body(bladeVisualMapQueryService.countByCriteria(criteria));
    }

    /**
     * {@code GET  /map/detail} : ID查询实体 bladeVisualMap.
     *
     * @param id the id of the bladeVisualMapDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the bladeVisualMapDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/map/detail")
    public ResponseResult<BladeVisualMapDTO> getBladeVisualMap(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long queryId = id != null ? id : equalsId;
        log.debug("REST request to get BladeVisualMap : {}", queryId);
        Optional<BladeVisualMapDTO> bladeVisualMapDTO = bladeVisualMapService.findOne(queryId);
        if (bladeVisualMapDTO.isPresent()) {
            return new ResponseResult(200, bladeVisualMapDTO.get(), "", true);
        } else {
            return new ResponseResult(404, null, "未查询到数据", false);
        }
    }

    /**
     * {@code GET  /map/data} : ID查询实体 bladeVisualMap 地图数据.
     *
     * @param id the id of the bladeVisualMapDTO to retrieve.
     * @return the {@link String} with status {@code 200 (OK)} and with body the bladeVisualMapDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping(value = "/map/data", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getBladeVisualMapData(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long queryId = id != null ? id : equalsId;
        log.debug("REST request to get BladeVisualMap : {}", queryId);
        Optional<BladeVisualMapDTO> bladeVisualMapDTO = bladeVisualMapService.findOne(queryId);
        if (bladeVisualMapDTO.isPresent()) {
            return bladeVisualMapDTO.get().getData();
        }
        return "{}";
    }


    /**
     * {@code DELETE  /map/remove} : ID删除实体 bladeVisualMap.
     *
     * @param id the id of the bladeVisualMapDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @PostMapping("/map/remove")
    public ResponseResult<Void> deleteBladeVisualMap(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long deleteId = id != null ? id : equalsId;
        log.debug("REST request to delete BladeVisualMap : {}", deleteId);
        bladeVisualMapService.delete(deleteId);
        return new ResponseResult(200, null, "删除成功", true);
    }
}
